// pages/zhaoping/zhaoping.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    xueshen: true,
    txtlist: [],

    STUDENT_PHONE: '', //手机号

    POST_NAME: '', //姓名
    xingbie: true,
    nianji: true,


    POST_SEX: '',
    POST_AGE: '',
    POST_QX: '',
    POST_ADD: '',
    shangwang: '',
    sbwngdian: true,
    wangdiany: true,



    modalHidden: true,
    canshu: '',
    USER_ASSETS_IDCARD: '',
    ad_img: '',
    PRIZE_ID: '',
    choujiang: true,
    openid: '',
    OPEN_ID: '',
    SHARE_OPEN_ID: '',












    wang_d: '', //地点
    city: [], //市
    district: [], //区
    business: '', //网点
    shi_k: true,
    qu_k: true,
    wang_k: true,
    tishi: true,

    L2code: '',
    L3code: '',

    L2name: '',
    L3name: '',
    L4name: '',

  },



















  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
      })
    } else if (this.data.mobile.length < 11) {

      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        POST_PHONE: '',
        mobileFormat: false,
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        POST_PHONE: '',
        mobileFormat: false,
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },




  bouut() {
    this.setData({
      openid: app.globalData.openid,
    })

    console.log(this.data.openid);
    // if (this.data.POST_SEX == "" ||this.data.POST_AGE == "" ||this.data.POST_SALARY == "" || this.data.POST_WORK == "" ||this.data.POST_NAME == "" || this.data.STUDENT_PHONE == ""|| this.data.POST_QX == ""|| this.data.POST_ADD == "") {
      if (this.data.POST_NAME == "" || this.data.STUDENT_PHONE == "" ||this.data.L2name == ""||this.data.L3name == ""||this.data.L4name == "") {
      wx.showToast({
        title: '请完善信息',
        icon: 'none',
        duration: 1500
      })

    } else {
      wx.request({
        url: app.globalData.interface+'/shanyouhui/API_1030_addUserCouponAssets',
        method: 'POST',
        data: {
          USER_ASSETS_NAME: this.data.POST_NAME,
          USER_COUPON_PHONE: this.data.STUDENT_PHONE,

          USER_ADD: this.data.L2name+'_'+'_'+this.data.L3name+'_'+this.data.L4name,

          
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data);
          console.log(res);

          if (res.data.result == 'success') {
            wx.showModal({
              // title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后   
                  console.log('用户点击确定')
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx.showModal({
              // title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后   
                  console.log('用户点击确定')
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
    }
  },










 // 地区数据
 region() {
  wx.request({
    url: app.globalData.interface+'/newVersion/API_2029_outletPush',
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: res => {
      console.log(res.data.data);
      this.setData({
        city: res.data.data,
      })

    }
  })
},


but_shi() {
  this.setData({
    shi_k: false,

  })

  this.setData({
    jiami: app.globalData.jiami,
    OPEN_ID: app.globalData.openid,
    openid: app.globalData.openid,
    session_key: app.globalData.session_key,
  });

  console.log(this.data.session_key);
},

but_xian() {
  if (this.data.L2code == '') {
    wx.showToast({
      title: '请先选择市',
      icon: 'error',
      duration: 2000
    })
  } else {
    this.setData({
      qu_k: false
    })
  }
},

but_w() {

  if (this.data.L2code == '') {
    wx.showToast({
      title: '请先选择区',
      icon: 'error',
      duration: 2000
    })
  } else {
    this.setData({
      wang_k: false
    })
  }

},


//市
buttn_c(e) {
  console.log(e);
  console.log(e.currentTarget.dataset.item.district);
  console.log(e.currentTarget.dataset.item.L2code);
  this.setData({
    district: e.currentTarget.dataset.item.district,
    L2code: e.currentTarget.dataset.item.L2code,
    L2name: e.currentTarget.dataset.item.L2name,
    shi_k: true,
    L3name:'',
    L4name:'',

  })

},

buttn_x(e) {
  console.log(e);


  this.setData({
    L3code: e.currentTarget.dataset.item.L3code,
    L3name: e.currentTarget.dataset.item.L3name,
    qu_k: true,
    L4name:'',
  })
  this.Outlets()

},


buttn_w(e) {
  console.log(e);
  this.setData({
    L4name: e.currentTarget.dataset.item.L4name,
    EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,
    wang_k: true
  })


  this.search_but()


},

//网点
Outlets() {
  wx.request({
    url: app.globalData.interface+'/newVersion/API_2027_outletList',
    method: 'POST',
    data: {
      L2code: this.data.L2code,
      L3code: this.data.L3code
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: res => {
      console.log(res);
      console.log(res.data.data);
      this.setData({
        business: res.data.data
      })

    }
  })
},










search_but() {


  wx.request({
    url: app.globalData.interface+'/goods/API_9811_selectGoodsTuanGouList',
    method: 'POST',
    data: {
      EMPLOYEE_ID: this.data.EMPLOYEE_ID, //员工工号
      OPEN_ID: this.data.openid, //用户OPEN_ID
      session_key: this.data.session_key,
      OPEN_UUID: this.data.jiami
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: res => {
      console.log(res.data.data);

      this.setData({
        // information:liscop,
        information: res.data.data,
        // shij:res.data.data[0].WECHATORALIPAY,
      })
      console.log(this.data.information);

    }
  })
},

























  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
      this.setData({
        openid: app.globalData.openid,
        jiami: app.globalData.jiami,
        OPEN_ID: app.globalData.openid,
        session_key: app.globalData.session_key,
        CUSTOMER_ID: app.globalData.CUSTOMER_ID,
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


    var that = this

    if (that.data.jiami == '' || that.data.session_key == '') {

      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) { //wx.login获取code。
            console.log(res.code);
            //发起网络请求

            wx.request({
              url: app.globalData.interface+'/post/API_1062_appGetOpenid',
              // url: 'http://192.168.3.173:8081/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                if (res.data.result != 'success') {
                  console.log(1);
                  // wx.showToast({
                  //   title: '请获取手机号',
                  //   duration: 1000
                  // })
                } else {
                  var openid = res.data.openid;
                  app.globalData.openid = openid;

                  console.log(res.data.openid);
                  that.setData({
                    openid: res.data.openid
                  })
                  // that.scanCod()

                  that.setData({
                    phone: app.globalData.phone,
                    CUSTOMER_ID: app.globalData.CUSTOMER_ID,
                    jiami: app.globalData.jiami,
                    OPEN_ID: app.globalData.openid,
                    openid: app.globalData.openid,
                    session_key: app.globalData.session_key,
                  })
                  console.log(that.data.phone);
                  console.log(that.data.jiami);
                  console.log(that.data.openid);
                  console.log(that.data.session_key);
                }

              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    } else {

      console.log('成功');
    }





    this.region()


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  }


})